home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / java / awt / Rectangle.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  26.0 KB  |  767 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)Rectangle.java    1.45 98/10/19
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.awt;
  16.  
  17. import java.awt.geom.Rectangle2D;
  18.  
  19. /**
  20.  * A <code>Rectangle</code> specifies an area in a coordinate space that is 
  21.  * enclosed by the <code>Rectangle</code> object's top-left point 
  22.  * (<i>x</i>, <i>y</i>) 
  23.  * in the coordinate space, its width, and its height. 
  24.  * <p>
  25.  * A <code>Rectangle</code> object's <code>width</code> and
  26.  * <code>height</code> are <code>public</code> fields. The constructors 
  27.  * that create a <code>Rectangle</code>, and the methods that can modify 
  28.  * one, do not prevent setting a negative value for width or height. 
  29.  * <p>
  30.  * A <code>Rectangle</code> whose width or height is negative is considered 
  31.  * empty. If the <code>Rectangle</code> is empty, then the  
  32.  * <code>isEmpty</code> method returns <code>true</code>. No point can be 
  33.  * contained by or inside an empty <code>Rectangle</code>.  The 
  34.  * values of <code>width</code> and <code>height</code>, however, are still 
  35.  * valid.  An empty <code>Rectangle</code> still has a location in the 
  36.  * coordinate space, and methods that change its size or location remain 
  37.  * valid. The behavior of methods that operate on more than one 
  38.  * <code>Rectangle</code> is undefined if any of the participating 
  39.  * <code>Rectangle</code> objects has a negative 
  40.  * <code>width</code> or <code>height</code>. These methods include 
  41.  * <code>intersects</code>, <code>intersection</code>, and 
  42.  * <code>union</code>. 
  43.  *
  44.  * @version     1.39, 06/24/98
  45.  * @author     Sami Shaio
  46.  * @since       JDK1.0
  47.  */
  48. public class Rectangle extends Rectangle2D
  49.     implements Shape, java.io.Serializable
  50. {
  51.  
  52.     /**
  53.      * The <i>x</i> coordinate of the <code>Rectangle</code>.
  54.      *
  55.      * @serial
  56.      * @see #setLocation(int, int) 
  57.      * @see #getLocation()
  58.      */
  59.     public int x;
  60.  
  61.     /**
  62.      * The <i>y</i> coordinate of the <code>Rectangle</code>.
  63.      * 
  64.      * @serial
  65.      * @see #setLocation(int, int)
  66.      * @see #getLocation()
  67.      */
  68.     public int y;
  69.  
  70.     /**
  71.      * The width of the <code>Rectangle</code>.
  72.      * @serial
  73.      * @see #setSize(int, int)
  74.      * @see #getSize()
  75.      * @since     JDK1.0.
  76.      */
  77.     public int width;
  78.  
  79.     /**
  80.      * The height of the <code>Rectangle</code>.
  81.      * 
  82.      * @serial
  83.      * @see #setSize(int, int)
  84.      * @see #getSize()
  85.      */
  86.     public int height;
  87.  
  88.     /*
  89.      * JDK 1.1 serialVersionUID 
  90.      */
  91.      private static final long serialVersionUID = -4345857070255674764L;
  92.        
  93.     /**
  94.      * Initialize JNI field and method IDs
  95.      */
  96.     private static native void initIDs();
  97.     
  98.     static {
  99.         /* ensure that the necessary native libraries are loaded */
  100.         Toolkit.loadLibraries();
  101.         initIDs();
  102.     }
  103.  
  104.     /**
  105.      * Constructs a new <code>Rectangle</code> whose top-left corner 
  106.      * is at (0, 0) in the coordinate space, and whose width and 
  107.      * height are both zero. 
  108.      */
  109.     public Rectangle() {
  110.         this(0, 0, 0, 0);
  111.     }
  112.  
  113.     /**
  114.      * Constructs a new <code>Rectangle</code>, initialized to match 
  115.      * the values of the specificed <code>Rectangle</code>.
  116.      * @param r  the <code>Rectangle</code> from which to copy initial values
  117.      *           to a newly constructed <code>Rectangle</code>
  118.      * @since JDK1.1
  119.      */
  120.     public Rectangle(Rectangle r) {
  121.         this(r.x, r.y, r.width, r.height);
  122.     }
  123.  
  124.     /**
  125.      * Constructs a new <code>Rectangle</code> whose top-left corner is 
  126.      * specified as
  127.      * (<code>x</code>, <code>y</code>) and whose width and height 
  128.      * are specified by the arguments of the same name. 
  129.      * @param     x, y the specified coordinates
  130.      * @param     width    the width of the <code>Rectangle</code>
  131.      * @param     height   the height of the <code>Rectangle</code>
  132.      */
  133.     public Rectangle(int x, int y, int width, int height) {
  134.     this.x = x;
  135.     this.y = y;
  136.     this.width = width;
  137.     this.height = height;
  138.     }
  139.  
  140.     /**
  141.      * Constructs a new <code>Rectangle</code> whose top-left corner 
  142.      * is at (0, 0) in the coordinate space, and whose width and 
  143.      * height are specified by the arguments of the same name. 
  144.      * @param width the width of the <code>Rectangle</code>
  145.      * @param height the height of the <code>Rectangle</code>
  146.      */
  147.     public Rectangle(int width, int height) {
  148.     this(0, 0, width, height);
  149.     }
  150.  
  151.     /**
  152.      * Constructs a new <code>Rectangle</code> whose top-left corner is 
  153.      * specified by the {@link Point} argument, and
  154.      * whose width and height are specified by the 
  155.      * {@link Dimension} argument. 
  156.      * @param p a <code>Point</code> that is the top-left corner of 
  157.      * the <code>Rectangle</code>
  158.      * @param d a <code>Dimension</code>, representing the 
  159.      * width and height of the <code>Rectangle</code>
  160.      */
  161.     public Rectangle(Point p, Dimension d) {
  162.     this(p.x, p.y, d.width, d.height);
  163.     }
  164.     
  165.     /**
  166.      * Constructs a new <code>Rectangle</code> whose top-left corner is the  
  167.      * specified <code>Point</code>, and whose width and height are both zero. 
  168.      * @param p a <code>Point</code> that is the top left corner 
  169.      * of the <code>Rectangle</code>
  170.      */
  171.     public Rectangle(Point p) {
  172.     this(p.x, p.y, 0, 0);
  173.     }
  174.     
  175.     /**
  176.      * Constructs a new <code>Rectangle</code> whose top left corner is  
  177.      * (0, 0) and whose width and height are specified  
  178.      * by the <code>Dimension</code> argument. 
  179.      * @param d a <code>Dimension</code>, specifying width and height
  180.      */
  181.     public Rectangle(Dimension d) {
  182.     this(0, 0, d.width, d.height);
  183.     }
  184.  
  185.     /**
  186.      * Returns the X coordinate of the bounding <code>Rectangle</code> in 
  187.      * <code>double</code> precision.
  188.      * @return the x coordinate of the bounding <code>Rectangle</code>. 
  189.      */
  190.     public double getX() {
  191.     return x;
  192.     }
  193.  
  194.     /**
  195.      * Returns the Y coordinate of the bounding <code>Rectangle</code> in 
  196.      * <code>double</code> precision.
  197.      * @return the y coordinate of the bounding <code>Rectangle</code>.    
  198.      */
  199.     public double getY() {
  200.     return y;
  201.     }
  202.  
  203.     /**
  204.      * Returns the width of the bounding <code>Rectangle</code> in 
  205.      * <code>double</code> precision.
  206.      * @return the width of the bounding <code>Rectangle</code>. 
  207.      */
  208.     public double getWidth() {
  209.     return width;
  210.     }
  211.  
  212.     /**
  213.      * Returns the height of the bounding <code>Rectangle</code> in 
  214.      * <code>double</code> precision.
  215.      * @return the height of the bounding <code>Rectangle</code>. 
  216.      */
  217.     public double getHeight() {
  218.     return height;
  219.     }
  220.  
  221.     /**
  222.      * Gets the bounding <code>Rectangle</code> of this <code>Rectangle</code>.
  223.      * <p>
  224.      * This method is included for completeness, to parallel the
  225.      * <code>getBounds</code> method of 
  226.      * {@link Component}.
  227.      * @return    a new <code>Rectangle</code>, equal to the 
  228.      * bounding <code>Rectangle</code> for this <code>Rectangle</code>.
  229.      * @see       java.awt.Component#getBounds
  230.      * @since     JDK1.1
  231.      */
  232.     public Rectangle getBounds() {
  233.     return new Rectangle(x, y, width, height);
  234.     }    
  235.  
  236.     /**
  237.      * Return the high precision bounding box of this rectangle.
  238.      * @since JDK1.2
  239.      */
  240.     public Rectangle2D getBounds2D() {
  241.     return new Rectangle(x, y, width, height);
  242.     }
  243.  
  244.     /**
  245.      * Sets the bounding <code>Rectangle</code> of this <code>Rectangle</code> 
  246.      * to match the specified <code>Rectangle</code>.
  247.      * <p>
  248.      * This method is included for completeness, to parallel the
  249.      * <code>setBounds</code> method of <code>Component</code>.
  250.      * @param r the specified <code>Rectangle</code>
  251.      * @see       java.awt.Component#setBounds(java.awt.Rectangle)
  252.      * @since     JDK1.1
  253.      */
  254.     public void setBounds(Rectangle r) {
  255.     setBounds(r.x, r.y, r.width, r.height);
  256.     }
  257.  
  258.     /**
  259.      * Sets the bounding <code>Rectangle</code> of this 
  260.      * <code>Rectangle</code> to the specified 
  261.      * <code>x</code>, <code>y</code>, <code>width</code>, 
  262.      * and <code>height</code>.
  263.      * <p>
  264.      * This method is included for completeness, to parallel the
  265.      * <code>setBounds</code> method of <code>Component</code>.
  266.      * @param x, y the new coordinates for the top-left
  267.      *                    corner of this <code>Rectangle</code>
  268.      * @param width the new width for this <code>Rectangle</code>
  269.      * @param height the new height for this <code>Rectangle</code>
  270.      * @see       java.awt.Component#setBounds(int, int, int, int)
  271.      * @since     JDK1.1
  272.      */
  273.     public void setBounds(int x, int y, int width, int height) {
  274.         reshape(x, y, width, height);
  275.     }    
  276.  
  277.     /**
  278.      * Sets the bounds of this <code>Rectangle</code> to the specified 
  279.      * <code>x</code>, <code>y</code>, <code>width</code>, 
  280.      * and <code>height</code>.
  281.      * This method is included for completeness, to parallel the
  282.      * <code>setBounds</code> method of <code>Component</code>.
  283.      * @param width the new width for the <code>Dimension</code> object
  284.      * @param height  the new height for the <code>Dimension</code> object
  285.      */
  286.     public void setRect(double x, double y, double width, double height) {
  287.     int x0 = (int) Math.floor(x);
  288.     int y0 = (int) Math.floor(y);
  289.     int x1 = (int) Math.ceil(x+width);
  290.     int y1 = (int) Math.ceil(y+height);
  291.     setBounds(x0, y0, x1-x0, y1-y0);
  292.     }    
  293.  
  294.     /**
  295.      * @deprecated As of JDK version 1.1,
  296.      * replaced by <code>setBounds(int, int, int, int)</code>.
  297.      */
  298.     public void reshape(int x, int y, int width, int height) {
  299.     this.x = x;
  300.     this.y = y;
  301.     this.width = width;
  302.     this.height = height;
  303.     }    
  304.  
  305.     /**
  306.      * Returns the location of this <code>Rectangle</code>.
  307.      * <p>
  308.      * This method is included for completeness, to parallel the
  309.      * <code>getLocation</code> method of <code>Component</code>.
  310.      * @return the <code>Point</code> that is the top-left corner of
  311.      *            this <code>Rectangle</code>. 
  312.      * @see       java.awt.Component#getLocation
  313.      * @since     JDK1.1
  314.      */
  315.     public Point getLocation() {
  316.     return new Point(x, y);
  317.     }    
  318.  
  319.     /**
  320.      * Moves this <code>Rectangle</code> to the specified location.
  321.      * <p>
  322.      * This method is included for completeness, to parallel the
  323.      * <code>setLocation</code> method of <code>Component</code>.
  324.      * @param p the <code>Point</code> specifying the new location
  325.      *                for this <code>Rectangle</code>
  326.      * @see       java.awt.Component#setLocation(java.awt.Point)
  327.      * @since     JDK1.1
  328.      */
  329.     public void setLocation(Point p) {
  330.     setLocation(p.x, p.y);
  331.     }    
  332.  
  333.     /**
  334.      * Moves this <code>Rectangle</code> to the specified location.
  335.      * <p>
  336.      * This method is included for completeness, to parallel the
  337.      * <code>setLocation</code> method of <code>Component</code>.
  338.      * @param x, y the coordinates of the new location
  339.      * @see       java.awt.Component#setLocation(int, int)
  340.      * @since     JDK1.1
  341.      */
  342.     public void setLocation(int x, int y) {
  343.     move(x, y);
  344.     }    
  345.  
  346.     /**
  347.      * @deprecated As of JDK version 1.1,
  348.      * replaced by <code>setLocation(int, int)</code>.
  349.      */
  350.     public void move(int x, int y) {
  351.     this.x = x;
  352.     this.y = y;
  353.     }    
  354.  
  355.     /**
  356.      * Translates this <code>Rectangle</code> the indicated distance,
  357.      * to the right along the x coordinate axis, and 
  358.      * downward along the y coordinate axis.
  359.      * @param dx the distance to move this <code>Rectangle</code> 
  360.      *                 along the x axis
  361.      * @param dy the distance to move this <code>Rectangle</code> 
  362.      *                 along the y axis
  363.      * @see       java.awt.Rectangle#setLocation(int, int)
  364.      * @see       java.awt.Rectangle#setLocation(java.awt.Point)
  365.      */
  366.     public void translate(int x, int y) {
  367.     this.x += x;
  368.     this.y += y;
  369.     }    
  370.  
  371.     /**
  372.      * Gets the size of this <code>Rectangle</code>, represented by 
  373.      * the returned <code>Dimension</code>.
  374.      * <p>
  375.      * This method is included for completeness, to parallel the
  376.      * <code>getSize</code> method of <code>Component</code>.
  377.      * @return a <code>Dimension</code>, representing the size of
  378.      *            this <code>Rectangle</code>.
  379.      * @see       java.awt.Component#getSize
  380.      * @since     JDK1.1
  381.      */
  382.     public Dimension getSize() {
  383.     return new Dimension(width, height);
  384.     }    
  385.  
  386.     /**
  387.      * Sets the size of this <code>Rectangle</code> to match the 
  388.      * specified <code>Dimension</code>.
  389.      * <p>
  390.      * This method is included for completeness, to parallel the
  391.      * <code>setSize</code> method of <code>Component</code>.
  392.      * @param d the new size for the <code>Dimension</code> object
  393.      * @see       java.awt.Component#setSize(java.awt.Dimension)
  394.      * @since     JDK1.1
  395.      */
  396.     public void setSize(Dimension d) {
  397.     setSize(d.width, d.height);
  398.     }    
  399.  
  400.     /**
  401.      * Sets the size of this <code>Rectangle</code> to the specified 
  402.      * width and height.
  403.      * <p>
  404.      * This method is included for completeness, to parallel the
  405.      * <code>setSize</code> method of <code>Component</code>.
  406.      * @param width the new width for this <code>Rectangle</code>
  407.      * @param height the new height for this <code>Rectangle</code>
  408.      * @see       java.awt.Component#setSize(int, int)
  409.      * @since     JDK1.1
  410.      */
  411.     public void setSize(int width, int height) {
  412.         resize(width, height);
  413.     }    
  414.  
  415.     /**
  416.      * @deprecated As of JDK version 1.1,
  417.      * replaced by <code>setSize(int, int)</code>.
  418.      */
  419.     public void resize(int width, int height) {
  420.     this.width = width;
  421.     this.height = height;
  422.     }    
  423.  
  424.     /**
  425.      * Checks whether or not this <code>Rectangle</code> contains the 
  426.      * specified <code>Point</code>.
  427.      * @param p the <code>Point</code> to test
  428.      * @return    <code>true</code> if the <code>Point</code> 
  429.      *            (<i>x</i>, <i>y</i>) is inside this 
  430.      *           <code>Rectangle</code>; 
  431.      *            <code>false</code> otherwise.
  432.      * @since     JDK1.1
  433.      */
  434.     public boolean contains(Point p) {
  435.     return contains(p.x, p.y);
  436.     }
  437.  
  438.     /**
  439.      * Checks whether or not this <code>Rectangle</code> contains the 
  440.      * point at the specified location
  441.      * (<i>x</i>, <i>y</i>).
  442.      * @param  x, y  the specified coordinates
  443.      * @return    <code>true</code> if the point 
  444.      *            (<i>x</i>, <i>y</i>) is inside this 
  445.      *          <code>Rectangle</code>; 
  446.      *            <code>false</code> otherwise.
  447.      * @since     JDK1.1
  448.      */
  449.     public boolean contains(int x, int y) {
  450.     return inside(x, y);
  451.     }
  452.  
  453.     /**
  454.      * Checks whether or not this <code>Rectangle</code> entirely contains 
  455.      * the specified <code>Rectangle</code>.
  456.      * @param     r   the specified <code>Rectangle</code>
  457.      * @return    <code>true</code> if the <code>Rectangle</code> 
  458.      *            is contained entirely inside this <code>Rectangle</code>; 
  459.      *            <code>false</code> otherwise.
  460.      * @since     JDK1.1
  461.      */
  462.     public boolean contains(Rectangle r) {
  463.     return contains(r.x, r.y, r.width, r.height);
  464.     }
  465.  
  466.     /**
  467.      * Checks whether this <code>Rectangle</code> entirely contains 
  468.      * the <code>Rectangle</code>
  469.      * at the specified location (<i>X</i>, <i>Y</i>) with the
  470.      * specified dimensions (<i>W</i>, <i>H</i>).
  471.      * @param     x, y  the specified coordinates
  472.      * @param     W   the width of the <code>Rectangle</code>
  473.      * @param     H   the height of the <code>Rectangle</code>
  474.      * @return    <code>true</code> if the <code>Rectangle</code> specified by
  475.      *            (<i>X</i>, <i>Y</i>, <i>W</i>, <i>H</i>)
  476.      *            is entirely enclosed inside this <code>Rectangle</code>; 
  477.      *            <code>false</code> otherwise.
  478.      * @since     JDK1.1
  479.      */
  480.     public boolean contains(int X, int Y, int W, int H) {
  481.     int width = this.width;
  482.     int height = this.height;
  483.     if (width <= 0 || height <= 0 || W <= 0 || H <= 0) {
  484.         return false;
  485.     }
  486.     int x = this.x;
  487.     int y = this.y;
  488.     return (X >= x &&
  489.         Y >= y &&
  490.         X + W <= x + width &&
  491.         Y + H <= y + height);
  492.     }
  493.  
  494.     /**
  495.      * @deprecated As of JDK version 1.1,
  496.      * replaced by <code>contains(int, int)</code>.
  497.      */
  498.     public boolean inside(int x, int y) {
  499.     return (x >= this.x) && ((x - this.x) < this.width) && (y >= this.y) && ((y-this.y) < this.height);
  500.     }
  501.  
  502.     /**
  503.      * Determines whether or not this <code>Rectangle</code> and the specified 
  504.      * <code>Rectangle</code> intersect. Two rectangles intersect if 
  505.      * their intersection is nonempty. 
  506.      * @param r the specified <code>Rectangle</code>
  507.      * @return    <code>true</code> if the specified <code>Rectangle</code> 
  508.      *            and this <code>Rectangle</code> insersect; 
  509.      *            <code>false</code> otherwise.
  510.      */
  511.     public boolean intersects(Rectangle r) {
  512.     return !((r.x + r.width <= x) ||
  513.          (r.y + r.height <= y) ||
  514.          (r.x >= x + width) ||
  515.          (r.y >= y + height));
  516.     }
  517.  
  518.     /**
  519.      * Computes the intersection of this <code>Rectangle</code> with the 
  520.      * specified <code>Rectangle</code>. Returns a new <code>Rectangle</code> 
  521.      * that represents the intersection of the two rectangles.
  522.      * @param     r   the specified <code>Rectangle</code>
  523.      * @return    the largest <code>Rectangle</code> contained in both the 
  524.      *            specified <code>Rectangle</code> and in 
  525.      *          this<code>Rectangle</code>.
  526.      */
  527.     public Rectangle intersection(Rectangle r) {
  528.     int x1 = Math.max(x, r.x);
  529.     int x2 = Math.min(x + width, r.x + r.width);
  530.     int y1 = Math.max(y, r.y);
  531.     int y2 = Math.min(y + height, r.y + r.height);
  532.     return new Rectangle(x1, y1, x2 - x1, y2 - y1);
  533.     }
  534.  
  535.     /**
  536.      * Computes the union of this <code>Rectangle</code> with the 
  537.      * specified <code>Rectangle</code>. Returns a new 
  538.      * <code>Rectangle</code> that 
  539.      * represents the union of the two rectangles
  540.      * @param r the specified <code>Rectangle</code>
  541.      * @return    the smallest <code>Rectangle</code> containing both 
  542.      *          the specified <code>Rectangle</code> and this 
  543.      *          <code>Rectangle</code>.
  544.      */
  545.     public Rectangle union(Rectangle r) {
  546.     int x1 = Math.min(x, r.x);
  547.     int x2 = Math.max(x + width, r.x + r.width);
  548.     int y1 = Math.min(y, r.y);
  549.     int y2 = Math.max(y + height, r.y + r.height);
  550.     return new Rectangle(x1, y1, x2 - x1, y2 - y1);
  551.     }
  552.  
  553.     /**
  554.      * Adds a point, specified by the integer arguments <code>newx</code>
  555.      * and <code>newy</code>, to this <code>Rectangle</code>. The 
  556.      * resulting <code>Rectangle</code> is
  557.      * the smallest <code>Rectangle</code> that contains both the 
  558.      * original <code>Rectangle</code> and the specified point.
  559.      * <p>
  560.      * After adding a point, a call to <code>contains</code> with the 
  561.      * added point as an argument does not necessarily return
  562.      * <code>true</code>. The <code>contains</code> method does not 
  563.      * return <code>true</code> for points on the right or bottom 
  564.      * edges of a <code>Rectangle</code>. Therefore, if the added point 
  565.      * falls on the right or bottom edge of the enlarged 
  566.      * <code>Rectangle</code>, <code>contains</code> returns 
  567.      * <code>false</code> for that point.
  568.      * @param newx, newy   the coordinates of the new point
  569.      */
  570.     public void add(int newx, int newy) {
  571.     int x1 = Math.min(x, newx);
  572.     int x2 = Math.max(x + width, newx);
  573.     int y1 = Math.min(y, newy);
  574.     int y2 = Math.max(y + height, newy);
  575.     x = x1;
  576.     y = y1;
  577.     width = x2 - x1;
  578.     height = y2 - y1;
  579.     }
  580.  
  581.     /**
  582.      * Adds the specified <code>Point</code> to this 
  583.      * <code>Rectangle</code>. The resulting <code>Rectangle</code> 
  584.      * is the smallest <code>Rectangle</code> that contains both the 
  585.      * original <code>Rectangle</code> and the specified 
  586.      * <code>Point</code>.
  587.      * <p>
  588.      * After adding a <code>Point</code>, a call to <code>contains</code> 
  589.      * with the added <code>Point</code> as an argument does not 
  590.      * necessarily return <code>true</code>. The <code>contains</code> 
  591.      * method does not return <code>true</code> for points on the right 
  592.      * or bottom edges of a <code>Rectangle</code>. Therefore if the added 
  593.      * <code>Point</code> falls on the right or bottom edge of the 
  594.      * enlarged <code>Rectangle</code>, <code>contains</code> returns 
  595.      * <code>false</code> for that <code>Point</code>.
  596.      * @param pt the new <code>Point</code> to add to this 
  597.      *           <code>Rectangle</code>
  598.      */
  599.     public void add(Point pt) {
  600.     add(pt.x, pt.y);
  601.     }
  602.  
  603.     /**
  604.      * Adds a <code>Rectangle</code> to this <code>Rectangle</code>. 
  605.      * The resulting <code>Rectangle</code> is the union of the two
  606.      * rectangles. 
  607.      * @param  r the specified <code>Rectangle</code>
  608.      */
  609.     public void add(Rectangle r) {
  610.     int x1 = Math.min(x, r.x);
  611.     int x2 = Math.max(x + width, r.x + r.width);
  612.     int y1 = Math.min(y, r.y);
  613.     int y2 = Math.max(y + height, r.y + r.height);
  614.     x = x1;
  615.     y = y1;
  616.     width = x2 - x1;
  617.     height = y2 - y1;
  618.     }
  619.  
  620.     /**
  621.      * Resizes the <code>Rectangle</code> both horizontally and vertically.
  622.      * <p>
  623.      * This method modifies the <code>Rectangle</code> so that it is 
  624.      * <code>h</code> units larger on both the left and right side, 
  625.      * and <code>v</code> units larger at both the top and bottom. 
  626.      * <p>
  627.      * The new <code>Rectangle</code> has (<code>x - h</code>, 
  628.      * <code>y - v</code>) as its top-left corner, a 
  629.      * width of 
  630.      * <code>width</code> <code>+</code> <code>2h</code>, 
  631.      * and a height of 
  632.      * <code>height</code> <code>+</code> <code>2v</code>. 
  633.      * <p>
  634.      * If negative values are supplied for <code>h</code> and 
  635.      * <code>v</code>, the size of the <code>Rectangle</code> 
  636.      * decreases accordingly. 
  637.      * The <code>grow</code> method does not check whether the resulting 
  638.      * values of <code>width</code> and <code>height</code> are 
  639.      * non-negative. 
  640.      * @param h the horizontal expansion
  641.      * @param v the vertical expansion
  642.      */
  643.     public void grow(int h, int v) {
  644.     x -= h;
  645.     y -= v;
  646.     width += h * 2;
  647.     height += v * 2;
  648.     }
  649.  
  650.     /**
  651.      * Determines whether or not this <code>Rectangle</code> is empty. A 
  652.      * <code>Rectangle</code> is empty if its width or its height is less 
  653.      * than or equal to zero. 
  654.      * @return     <code>true</code> if this <code>Rectangle</code> is empty; 
  655.      *             <code>false</code> otherwise.
  656.      */
  657.     public boolean isEmpty() {
  658.     return (width <= 0) || (height <= 0);
  659.     }
  660.  
  661.     /**
  662.      * Determines where the specified coordinates lie with respect
  663.      * to this <code>Rectangle</code>.  
  664.      * This method computes a binary OR of the appropriate mask values
  665.      * indicating, for each side of this <code>Rectangle</code>, 
  666.      * whether or not the specified coordinates are on the same side of the
  667.      * edge as the rest of this <code>Rectangle</code>.
  668.      * @param x, y the specified coordinates
  669.      * @return the logical OR of all appropriate out codes.
  670.      * @see #OUT_LEFT
  671.      * @see #OUT_TOP
  672.      * @see #OUT_RIGHT
  673.      * @see #OUT_BOTTOM
  674.      * @since JDK1.2
  675.      */
  676.     public int outcode(double x, double y) {
  677.     int out = 0;
  678.     if (this.width <= 0) {
  679.         out |= OUT_LEFT | OUT_RIGHT;
  680.     } else if (x < this.x) {
  681.         out |= OUT_LEFT;
  682.     } else if (x > this.x + this.width) {
  683.         out |= OUT_RIGHT;
  684.     }
  685.     if (this.height <= 0) {
  686.         out |= OUT_TOP | OUT_BOTTOM;
  687.     } else if (y < this.y) {
  688.         out |= OUT_TOP;
  689.     } else if (y > this.y + this.height) {
  690.         out |= OUT_BOTTOM;
  691.     }
  692.     return out;
  693.     }
  694.  
  695.     /**
  696.      * Returns a new {@link Rectangle2D} object
  697.      * representing the intersection of this <code>Rectangle</code> with the 
  698.      * specified <code>Rectangle2D</code>.
  699.      * @param r the <code>Rectangle2D</code> to be intersected 
  700.      *             with this <code>Rectangle</code>
  701.      * @return    the largest <code>Rectangle2D</code> contained in both the 
  702.      *            specified <code>Rectangle2D</code> and in 
  703.      *          this <code>Rectangle</code>.
  704.      * @since JDK1.2
  705.      */
  706.     public Rectangle2D createIntersection(Rectangle2D r) {
  707.     if (r instanceof Rectangle) {
  708.         return intersection((Rectangle) r);
  709.     }
  710.     Rectangle2D dest = new Rectangle2D.Double();
  711.     Rectangle2D.intersect(this, r, dest);
  712.     return dest;
  713.     }
  714.  
  715.     /**
  716.      * Returns a new <code>Rectangle2D</code> object representing the
  717.      * union of this <code>Rectangle</code> with the specified 
  718.      * <code>Rectangle2D</code>.
  719.      * @param r the <code>Rectangle2D</code> to be combined with
  720.      *           this <code>Rectangle</code>
  721.      * @return    the smallest <code>Rectangle2D</code> containing 
  722.      *           both the specified <code>Rectangle2D</code> and this 
  723.      *            <code>Rectangle</code>.
  724.      * @since JDK1.2
  725.      */
  726.     public Rectangle2D createUnion(Rectangle2D r) {
  727.     if (r instanceof Rectangle) {
  728.         return union((Rectangle) r);
  729.     }
  730.     Rectangle2D dest = new Rectangle2D.Double();
  731.     Rectangle2D.union(this, r, dest);
  732.     return dest;
  733.     }
  734.  
  735.     /**
  736.      * Checks whether two rectangles are equal.
  737.      * <p>
  738.      * The result is <code>true</code> if and only if the argument is not 
  739.      * <code>null</code> and is a <code>Rectangle</code> object that has the 
  740.      * same top-left corner, width, and height as this <code>Rectangle</code>. 
  741.      * @param obj the <code>Object</code> to compare with
  742.      *                this <code>Rectangle</code>
  743.      * @return    <code>true</code> if the objects are equal; 
  744.      *            <code>false</code> otherwise.
  745.      */
  746.     public boolean equals(Object obj) {
  747.     if (obj instanceof Rectangle) {
  748.         Rectangle r = (Rectangle)obj;
  749.         return ((x == r.x) &&
  750.             (y == r.y) &&
  751.             (width == r.width) &&
  752.             (height == r.height));
  753.     }
  754.     return super.equals(obj);
  755.     }
  756.  
  757.     /**
  758.      * Returns a <code>String</code> representing this 
  759.      * <code>Rectangle</code> and its values.
  760.      * @return a <code>String</code> representing this 
  761.      *               <code>Rectangle</code> object's coordinate and size values.
  762.      */
  763.     public String toString() {
  764.     return getClass().getName() + "[x=" + x + ",y=" + y + ",width=" + width + ",height=" + height + "]";
  765.     }
  766. }
  767.